package com.hotel.BL;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.ReasonDetail;

public class ReasonDetailLogic extends AbstractBL {

	public ReasonDetail GetReasonDetail(long id)
	{
		Key key = KeyFactory.createKey(EntityName.ReasonDetail, id);
		try {
			Entity lang = datastore.get(key);
			ReasonDetail c = new ReasonDetail();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}

	public List<ReasonDetail> GetAllReasonDetail(long hotelId)
	{
		Query q = new Query(EntityName.ReasonDetail);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		
		q.setFilter(filterHotel);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	
	public List<ReasonDetail> GetList(List<Entity> entities)
	{
		List<ReasonDetail> list = new ArrayList<ReasonDetail>();
		for(Entity e : entities)
		{
			ReasonDetail a = new ReasonDetail();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}
	
	public ReasonDetail AddReasonDetail(long hotelId,String reservationIdString,String reasonString,String detail,Date dateTime,String massage) throws EntityNotFoundException{
		ReasonDetail rd = new ReasonDetail();
		rd.setHotelId(hotelId);
		rd.setReservationIdString(reservationIdString);
		rd.setReasonString(reasonString);
		rd.setDetail(detail);
		rd.setCancelDate(dateTime);
		rd.setEmailMassage(massage);
		Key k = datastore.put(rd.getEntity());
		rd.setId(k.getId());
		rd.setKey(k);

		return rd;
	}
	
	public ReasonDetail DeleteReasonDetail(long id) throws EntityNotFoundException{
		Key key = KeyFactory.createKey(EntityName.ReasonDetail, id);
		datastore.delete(key);
		return null;
	}
	
	public ReasonDetail EditReservation(long hotelId, long resId, String arraResId)
			throws EntityNotFoundException, ParseException {
		
		ReasonDetail rsv = GetReason(resId);
		rsv.setReservationIdString(arraResId);
		Key k = datastore.put(rsv.getEntity());
		rsv.setId(k.getId());
		rsv.setKey(k);

		return rsv;
	}
	public ReasonDetail GetReason(long id) {
		Key key = KeyFactory.createKey(EntityName.ReasonDetail, id);
		try {
			Entity lang = datastore.get(key);
			ReasonDetail c = new ReasonDetail();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
}
